import java.util.Arrays;

public class TextDemo {
    public static void bubbleSort(int[] array){
        int lastIndex = array.length-1;
        int swapIndex = -1;
        boolean flag = true;
        while(flag){
            flag = false;
            for(int i = 0;i<lastIndex;i++){
                if(array[i]>array[i+1]){
                    swap(array,i,i+1);
                    flag = true;
                    swapIndex = i;
                }
            }
            lastIndex = swapIndex;
        }
    }
    private static void swap(int[] array,int x,int y){
        int tem = array[x];
        array[x] = array[y];
        array[y] = tem;
    }
    private static void swap1(int[] array,int x){
        array[x+1] = array[x+1]+array[x];
        array[x] = array[x+1]-array[x];
        array[x+1] = array[x+1]-array[x];
    }
    public static void insertSort(int[] array){
        int maxIndex = -1;
        int minIndex = -1;
        for(int i = 0;i<array.length/2;i++){
            maxIndex = i;
            minIndex = i;
            for(int j =i;j<array.length-i;j++){
                if(array[minIndex]>array[j]){
                    minIndex = j;
                }
                if(array[maxIndex]<array[j]){
                    maxIndex = j;
                }
            }
            if(minIndex==maxIndex){
                break;
            }
            swap(array,minIndex,i);
            if(i==maxIndex){
                maxIndex = minIndex;
            }
            int lastIndex = array.length-1-i;
            swap(array,lastIndex,maxIndex);
        }
    }

    public static void main(String[] args) {
        int[] array = new int[]{9,2,6,4,0,1,4,2,3,7,3,5,8};
        System.out.println(Arrays.toString(array));
        insertSort(array);
        System.out.println(Arrays.toString(array));
    }
}
